package com.cjd.common.adapter

import android.view.View
import android.view.ViewGroup
import com.cjd.common.holder.BaseGridAndListHolder

/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2019/9/24
 * description
 */
abstract class BaseGridAndListAdapter<T> :
    BaseGridAndListArrayAdapter<T, BaseGridAndListHolder>() {
    override fun getView(position: Int, convertView1: View?, parent: ViewGroup): View {
        var convertView = convertView1
        var holder: BaseGridAndListHolder? = null

        if (convertView == null) {
            holder = onCreateItemViewHolder(parent, position)
            convertView = holder.itemView
            convertView.tag = holder
        } else {
            if (hasCacheViewHolder()) {
                holder = convertView.tag as BaseGridAndListHolder
            } else {
                holder = onCreateItemViewHolder(parent, position)
                convertView = holder.itemView
            }
        }
        onBindItemViewHolder(holder, position)
        return convertView
    }

    protected abstract fun onCreateItemViewHolder(
        parent: ViewGroup,
        position: Int
    ): BaseGridAndListHolder


    protected abstract fun onBindItemViewHolder(holder: BaseGridAndListHolder, position: Int)

    /**
     * 是否缓存 view 及 holder
     *
     * @return boolean
     */
    protected fun hasCacheViewHolder(): Boolean {
        return true
    }
}